﻿using DevExpress.XtraBars.Docking2010.Views;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraBars.Docking;
using DevExpress.XtraBars.Docking2010.Views.Widget;
using XCI.Core;
using XCI.Extensions;
using XCI.Helper;
using XCI.Windows.Widgets;

namespace XCI.Windows
{
    [ToolboxItem(false)]
    [WidgetCaption("日历")]
    public partial class ClockWidget : BaseWidget
    {
        private Document _document;
        private Timer timer;
        public ClockWidget()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        /// <param name="document">文档对象</param>
        public override void Init(BaseDocument document)
        {
            this._document = (Document)document;
            var interval = ClockWidgetConfig.Get().TimerInterval;
            InitButton();
            InitTimer(interval);
        }

        /// <summary>
        /// 设置事件
        /// </summary>
        public void SettingAction()
        {
            var configInterval = ClockWidgetConfig.Get().TimerInterval;
            var result = MessageBoxHelper.ShowPrompt("请输入刷新频率(毫秒)", configInterval.ToString());
            if (result.IsEmpty()) return;
            var interval = result.ToInt(-1);
            if (interval < 0)
            {
                MessageBoxHelper.ShowWarn("请输入一个不小于1000的整数");
                return;
            }
            if (interval < 1000)
            {
                MessageBoxHelper.ShowWarn("刷新频率不能小于1000");
                return;
            }

            ClockWidgetConfig.Get().TimerInterval = interval;
            ClockWidgetConfig.Save();
            InitTimer(interval);
        }

        private void InitButton()
        {
            #region 设置按钮

            var settingButton = new CustomHeaderButton("设置", false, new CustomHeaderButtonImageOptions
            {
                SvgImage = Properties.Resources.ActionSetting,
                SvgImageSize = new Size(12, 12),
                SvgImageColorizationMode = SvgImageColorizationMode.Full
            })
            { ToolTip = "设置" };
            _document.CustomHeaderButtons.Add(settingButton);

            #endregion

            #region 刷新按钮

            var refreshButton = new CustomHeaderButton("刷新", false, new CustomHeaderButtonImageOptions
            {
                SvgImage = Properties.Resources.ActionSync,
                SvgImageSize = new Size(12, 12),
                SvgImageColorizationMode = SvgImageColorizationMode.Full
            })
            { ToolTip = "刷新" };
            _document.CustomHeaderButtons.Add(refreshButton);

            #endregion

            #region CustomButtonClick

            _document.CustomButtonClick += (o, e) =>
            {
                if (e.Button.Properties.Caption == "刷新")
                {
                    DoAction();
                }
                else if (e.Button.Properties.Caption == "设置")
                {
                    this.SettingAction();
                }
            };

            #endregion
        }

        private void InitTimer(int interval)
        {
            if (interval < 1000) interval = 1000;
            if (timer == null)
            {
                timer = new Timer();
                timer.Tick += (o, e) => DoAction();
            }
            timer.Stop();
            timer.Interval = interval;
            timer.Start();
            DoAction();
        }

        private void DoAction()
        {
            var currentDate = DateTime.Now;
            var cal = new ChinaCalendar(currentDate);

            labelControl1.Text = $"<size=24><b>{currentDate:T}</b></size>" +
                                 "<br>" +
                                 "<size=16>" +
                                 $"{currentDate:D} {DateTimeHelper.GetWeekDay()}" +
                                 $"<br>{cal.ChineseMonthDateString} {cal.AnimalString}年</size>";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                timer?.Stop();
                timer?.Dispose();
                components.Dispose();
            }
            base.Dispose(disposing);
        }
    }

    /// <summary>
    /// 日历部件配置
    /// </summary>
    public class ClockWidgetConfig
    {
        private static readonly JsonFile<ClockWidgetConfig> JsonFile = new JsonFile<ClockWidgetConfig>(IOHelper.GetRootFilePath("clockWidgetConfig.json"));

        /// <summary>
        /// 上传密码
        /// </summary>
        public int TimerInterval { get; set; } = 1;

        /// <summary>
        /// 获取配置
        /// </summary>
        public static ClockWidgetConfig Get()
        {
            return JsonFile.Data;
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        public static void Save()
        {
            JsonFile.Save();
        }
    }
}
